package com.salton123.ui.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.salton123.feature.IFeature

/**
 * Time:6/2/21 11:38 AM
 * Author:
 * Description:
 */
abstract class LifeDelegateDialogFragment(val componentLife: ComponentLife = ComponentLife()) : DialogFragment(),
    IComponentLife by componentLife {
    abstract fun getLayoutId(): Int
    open fun isAsyncContentView(): Boolean = true
    abstract fun initViewAndData(savedInstanceState: Bundle?) //初始化数据在View之后
    open fun initListener() {}
    open fun initLogic() {}
    private var features: MutableList<IFeature> = mutableListOf()
    fun addFeature(feature: IFeature) {
        if (!features.contains(feature)) {
            features.add(feature)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        componentLife.with(this)
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return componentLife.getRootView()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        inflateContentView(isAsyncContentView(), getLayoutId()) {
            initViewAndData(savedInstanceState)
            initListener()
            initLogic()
            features.forEach { it.onBind() }
        }
    }
}